package jp.co.redfox.archclub.poker;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public enum Card {
    SPADE_A(Suits.spade, "A"),
    SPADE_2(Suits.spade, "2"),
    SPADE_3(Suits.spade, "3"),
    SPADE_4(Suits.spade, "4"),
    SPADE_5(Suits.spade, "5"),
    SPADE_6(Suits.spade, "6"),
    SPADE_7(Suits.spade, "7"),
    SPADE_8(Suits.spade, "8"),
    SPADE_9(Suits.spade, "9"),
    SPADE_10(Suits.spade, "10"),
    SPADE_J(Suits.spade, "J"),
    SPADE_Q(Suits.spade, "Q"),
    SPADE_K(Suits.spade, "K"),
    CLUB_A(Suits.clover, "A"),
    CLUB_2(Suits.clover, "2"),
    CLUB_3(Suits.clover, "3"),
    CLUB_4(Suits.clover, "4"),
    CLUB_5(Suits.clover, "5"),
    CLUB_6(Suits.clover, "6"),
    CLUB_7(Suits.clover, "7"),
    CLUB_8(Suits.clover, "8"),
    CLUB_9(Suits.clover, "9"),
    CLUB_10(Suits.clover, "10"),
    CLUB_J(Suits.clover, "J"),
    CLUB_Q(Suits.clover, "Q"),
    CLUB_K(Suits.clover, "K"),
    HEART_A(Suits.heart, "A"),
    HEART_2(Suits.heart, "2"),
    HEART_3(Suits.heart, "3"),
    HEART_4(Suits.heart, "4"),
    HEART_5(Suits.heart, "5"),
    HEART_6(Suits.heart, "6"),
    HEART_7(Suits.heart, "7"),
    HEART_8(Suits.heart, "8"),
    HEART_9(Suits.heart, "9"),
    HEART_10(Suits.heart, "10"),
    HEART_J(Suits.heart, "J"),
    HEART_Q(Suits.heart, "Q"),
    HEART_K(Suits.heart, "K"),
    DIAMOND_A(Suits.diamond, "A"),
    DIAMOND_2(Suits.diamond, "2"),
    DIAMOND_3(Suits.diamond, "3"),
    DIAMOND_4(Suits.diamond, "4"),
    DIAMOND_5(Suits.diamond, "5"),
    DIAMOND_6(Suits.diamond, "6"),
    DIAMOND_7(Suits.diamond, "7"),
    DIAMOND_8(Suits.diamond, "8"),
    DIAMOND_9(Suits.diamond, "9"),
    DIAMOND_10(Suits.diamond, "10"),
    DIAMOND_J(Suits.diamond, "J"),
    DIAMOND_Q(Suits.diamond, "Q"),
    DIAMOND_K(Suits.diamond, "K");
    
    public static final Suits[] RANK_SUITS = new Suits[] {
        Suits.clover,  // {♣, ♦, ♥, ♠}
        Suits.diamond,
        Suits.heart,
        Suits.spade,
    };
    public static final String[] RANK_NUMBERS = new String[] {
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "J",
        "Q",
        "K",
        "A",
    };

    

    public enum Suits{
        spade("♠"),
        diamond("♦"),
        heart("♥"),
        clover("♣");
        private String mark;
        private Suits(String mark){
            this.mark = mark;
        }
        public String toString(){
            return this.mark;
        }
    }
    
    private final String number;
    private final Suits suits;
    private static final List<String> cardNumRank = new ArrayList<>(13);
    static {
        cardNumRank.add("A");
        cardNumRank.add("2");
        cardNumRank.add("3");
        cardNumRank.add("4");
        cardNumRank.add("5");
        cardNumRank.add("6");
        cardNumRank.add("7");
        cardNumRank.add("8");
        cardNumRank.add("9");
        cardNumRank.add("10");
        cardNumRank.add("J");
        cardNumRank.add("Q");
        cardNumRank.add("K");
    }
    
    private Card(Suits suits, String number){
        this.suits = suits;
        this.number = number;
    }
    public String toString(){
        return "[" + suits + " " + number + "]";
    }
    
    public String getNumber() {
        return this.number;
    }
    
    public Suits getSuits() {
        return this.suits;
    }
    
    public static final Card getBestCard(List<Card> cardList) {
        Card currentBestCard = null;
        for (Card card : cardList) {
            if (currentBestCard == null) {
                currentBestCard = card;
                continue;
            }
            if(currentBestCard.getNumberStrength() < card.getNumberStrength()){
                currentBestCard = card;
                continue;                
            }
            else if(currentBestCard.getSimbolStrength() < card.getSimbolStrength()){
                currentBestCard = card;
                continue;
            }
                
            // TODO suitsで同じような実装
        }
        return currentBestCard;
    }

    public int getNumberStrength(){
        return Arrays.asList(RANK_NUMBERS).indexOf(this.getNumber());
    }
    public int getSimbolStrength(){
        return Arrays.asList(RANK_SUITS).indexOf(this.getSuits());
    }
    
    public static final Card getBestCard(Card... cards) {
        return getBestCard(Arrays.asList(cards));
    }
    
    public static void sort(List<Card> cards) {
        Comparator<Card> cardcomparator = new Comparator<Card>() {
            @Override
            public int compare(Card c1, Card c2) {
                return cardNumRank.indexOf(c1.getNumber()) - cardNumRank.indexOf(c2.getNumber());
            }
        };
        Collections.sort(cards, cardcomparator);
    }
    public boolean isJoker() {
        // TODO Auto-generated method stub
        return false;
    }
    public boolean sameNumber(Card other) {
        return this.getNumber().equals(other.getNumber());
    }
}
